<template>
  <div class="home">
    <div class="home-content">
      <div class="home-top-title">
        <img src="@/assets/images/datx.png" alt="">
        <div>DATx{{$t("h.wallet")}}</div>
      </div>
      <div class="table">
        <div v-if="istable">
          <el-table
            :data="tableData"
            style="width: 100%"
            @row-click="detail">
            <el-table-column
              prop=""
              label=""
              width="30">
            </el-table-column>
            <el-table-column
              prop="token"
              :label="'DATx'+this.$t('h.wallet')">
              <template slot-scope="scope">
                <div class="wallet">
                  <div><img src="@/assets/images/dataxpic.png" alt=""></div>
                  <div>DATX</div>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="name"
              :label="this.$t('h.account')">
            </el-table-column>
            <el-table-column
              prop="balance"
              :label="this.$t('h.balance')">
            </el-table-column>
            <el-table-column
              prop="status"
              :label="this.$t('h.status')">
              <template slot-scope="scope">
                <span v-if="scope.row.status==='creating'">{{$t("h.notcreated")}}</span>
                <span v-else-if="scope.row.status==='created'">{{$t("h.created")}}</span>
                <span v-else>{{$t("h.notime")}}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div v-else class="none">{{$t("h.nodata")}}</div>
        <div class="hand-wallet">
            <div @click="importWallethandle"><img src="@/assets/images/icon-import.png" alt="">{{$t("h.importWallet")}}</div>
          <div @click="addwallet"><img src="@/assets/images/icon-creat.png" alt="">{{$t("h.createdWallet")}}</div>
        </div>
      </div>
      <import-wallet v-if="showwallet" @showHand="showHid"></import-wallet>
      <success-import v-if="showSuccess" @successListen="successListen"></success-import>
    </div>
  </div>
</template>
<script>
  import importWallet from '@/components/importWallet'
  import {getAllMyAccount} from '../../service/datxService'
  import successImport from '@/components/successImport'
  import {getWalletBalance} from '../../api/chainApi'
  import _ from 'lodash'

  export default {
    components: {
      importWallet,
      successImport
    },
    data() {
      return {
        istable: false,
        showwallet: false,
        tableData: [],
        showSuccess: false,
      }
    },

    created() {
      this.getDatx();
    },
    methods: {
      // 导入成功
      successListen() {
        this.showSuccess = false;
        this.getDatx()
      },
      showHid(data, success) {
        this.showwallet = data;
        if (success)
          this.showSuccess = true;
      },
      importWallethandle() {
        this.showwallet = true;
        console.log(this.showwallet);
      },
      addwallet() {
        this.$router.push('add');
      },
      // 获取datx钱包表格
      getDatx() {
        let obj = getAllMyAccount()
        if (obj == null) {
          this.istable = false;
        } else {
          let table = [];
          _(obj).forEach(function (o) {
            o.balance='';
            getWalletBalance('DATX', o.name).then(res => {
              _(res.data).forEach(function (a) {
                if (a.token === 'DATX') {
                  o.balance = a.balance;
                }
              })
            });
            table.push(o)
          });
          this.tableData = table;
          this.istable = true;
        }
      },
      detail(row) {
        this.$router.push({path: '/account'});
        window.sessionStorage["walletdetail"] = JSON.stringify(row);
      }
    }
  }
</script>
<style lang="less" scoped>
  .home {
    padding-bottom: 40px;
    .home-title {
      color: #000000;
      opacity: 0.6;
      font-size: 30px;
      padding: 25px 0;
    }
    &-content {
      background: #fff;
      min-height: 300px;
      padding-bottom: 20px;
      .home-top-title {
        display: flex;
        align-items: center;
        padding: 30px;
        img {
          width: 34px;
          height: 34px;
        }
        div {
          font-size: 24px;
          padding-left: 8px;
        }
      }
    }
  }

  .none {
    text-align: center;
    margin: 40px 0;
    font-size: 16px;
  }

  .table {
    .wallet {
      display: flex;
      align-items: center;
      img {
        width: 28px;
        height: 28px;
        margin-right: 5px;
      }
    }
    .asset {
      div {
        span {
          color: #c5c7cb;
        }
      }
    }
    .hand-wallet {
      display: flex;
      justify-content: center;
      padding-top: 30px;
      div {
        width: 540px;
        height: 70px;
        margin-right: 15px;
        // opacity: 0.56;
        border-radius: 4px;
        border: dashed 1px #979797;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 20px;
        color: #98889b;
        cursor: pointer;
        img {
          width: 40px;
          height: 40px;
          margin-right: 20px;
        }
      }
      div:last-child {
        margin-right: 0;
      }
    }
  }
</style>
<style lang='less'>
  .table {
    .el-table__body-wrapper {
      tr {
        height: 135px !important;
      }
    }
  }
</style>


